<!--
  作者：xhr
  时间：2022年09月16日 21:40:14
-->

<template>
    <div style="padding: 10px">
        <!-- <div style="margin: 10px 0">
            <el-button type="primary" @click="add">新增</el-button>
            <el-button type="primary">导入</el-button>
          <el-button type="primary">导出</el-button>
                </div> -->
        <!-- <div style="margin: 10px 0">
                    <el-input v-model="search" placeholder="请输入内容" style="width: 20%" clearable></el-input>
                    <el-button type="primary" style="margin-left: 10px" @click="loading">查询</el-button>
                </div> -->
        <div style="margin: 10px 0">
            <el-popconfirm style="margin-right: 5px;" class="ml-5" confirm-button-text='好的' cancel-button-text='不用了'
                icon="el-icon-info" icon-color="red" title="您确定删除吗？" @confirm="delBath">
                <el-button plain class="el-icon-circle-close" type="danger" slot="reference">批量删除</el-button>
            </el-popconfirm>
            <el-input v-model="search" placeholder="学生用户名筛查" style="width: 20%" clearable @clear="reply"></el-input>
            <el-button type="primary" style="margin: 0 10px 0 5px" @click="searchByName">查询</el-button>
            <el-select v-model="value" filterable clearable @clear="reply" placeholder="考试筛选"
                :filter-method="selectCourse(value)">
                <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
            </el-select>
        </div>
        <el-table :data="tableData" border stripe>
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column sortable prop="id" label="ID"></el-table-column>
            <el-table-column label="考试">
                <template slot-scope="scope">
                    <span>{{ exams.find(v => v.id === scope.row.examId) ? exams.find(v =>
                        v.id === scope.row.examId).name : '大学生心理辅导' }}</span>
                </template>
            </el-table-column>
            <el-table-column label="用户">
                <template slot-scope="scope">
                    <span>{{ users.find(v => v.id === scope.row.userId) ? users.find(v =>
                        v.id === scope.row.userId).username : '' }}</span>
                </template>
            </el-table-column>
            <el-table-column prop="time" label="提交时间"></el-table-column>
            <el-table-column prop="state" label="状态"></el-table-column>
            <el-table-column prop="score" label="得分"></el-table-column>

            <el-table-column fixed="right" label="操作" width="150">
                <template slot-scope="scope">

                    <el-button @click="$router.push('/handlePaper?sp=' + scope.row.id)" size="mini">阅卷</el-button>
                    <el-popconfirm title="确定删除吗？" style="margin-left: 5px" @confirm="delate(scope.row.id)">
                        <el-button type="danger" size="mini" slot="reference">删除
                        </el-button>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>
        <div style="margin: 10px 0">
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
                :page-sizes="[5, 10, 50]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
                :total="total">
            </el-pagination>
        </div>


    </div>
</template>

<script>
import request from "@/utils/request";



export default {
    name: "StudentPaper",
    data() {
        return {

            form: {},
            total: 0,
            currentPage: 1,
            pageSize: 10,
            dialogVisible: false,
            tableData: [],
            users: [],
            exams: [],
            options: [],
            value: '',
            examId: null,
            search: '',
        };
    },
    created() {
        request.get("/user").then((res) => {
            this.users = res.data;
        });
        request.get("/exam").then((res) => {
            this.exams = res.data;
            this.exams.forEach(e => {
                var obj = {};
                obj.value = e.id;
                obj.label = e.name;
                this.options.push(obj);
            });
        });
        this.loading();
    },
    mounted() { },
    methods: {

        loading() {
            request
                .get("/studentPaper/page", {
                    params: {
                        pagenum: this.currentPage,
                        pagesize: this.pageSize,
                        examId: this.examId,

                    },
                })
                .then((res) => {
                    this.tableData = res.data.records;
                    this.total = res.data.total;
                });
        },
        handlePaper() {

        },
        // save() {


        //     if (this.form.id) {
        //         request.put("/course", this.form).then((res) => {
        //             console.log(res);
        //             if (res.code === "0") {
        //                 this.$message({
        //                     type: "success",
        //                     message: "更新成功",
        //                 });
        //             } else {
        //                 this.$message({
        //                     type: "error",
        //                     message: "更新失败",
        //                 });
        //             }
        //             this.loading();
        //             this.dialogVisible = false;

        //         });
        //     } else {

        //         request.post("/course", this.form).then((res) => {
        //             console.log(res);
        //             if (res.code === "0") {
        //                 this.$message({
        //                     type: "success",
        //                     message: "新增成功",
        //                 });
        //             } else {
        //                 this.$message({
        //                     type: "error",
        //                     message: "新增失败",
        //                 });
        //             }
        //             this.loading();
        //             this.dialogVisible = false;

        //         });
        //     }
        // },


        delate(id) {
            request.delete("/course/" + id).then((res) => {
                if (res.code === "0") {
                    this.$message({
                        type: "success",
                        message: "删除成功",
                    });
                } else {
                    this.$message({
                        type: "error",
                        message: "删除失败",
                    });
                }
            });
            this.loading();
        },
        handleSizeChange(pageSize) {
            this.pageSize = pageSize;
            this.loading();
        },
        handleCurrentChange(pageNum) {
            this.currentPage = pageNum;
            this.loading();
        },
        delBath() {
            // if (!this.ids.length) {
            //   this.$message.warning("请选择数据！")
            //   return
            // }
            // request.delete("/activity/delBatch/" + this.ids).then(res => {
            //   this.$message.success("批量删除成功！");
            //   this.loading()
            // })
        },
        reply() {
            this.search = '';
            this.examId = null;
            this.newArr = [];
            this.loading();
        },
        selectCourse(st) {
            if (st != "") {
                this.examId = st;
                this.loading();
            }
        },
        searchByName() {
            this.users.forEach(s=>{
                if(s.username==this.search){
                    const newArr=this.tableData.filter(e => e.userId==s.id);
                    this.tableData=newArr;
                }
            })
        },
    },
};
</script>

<style></style>